﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BLL;
using Model;
using Common;

namespace API.Controllers
{
    [RoutePrefix("api/BasicInformation")]
    public class BasicInformationController : ApiController
    {
        B_BasicInformation _bb;
        public BasicInformationController()
        {
            _bb = new B_BasicInformation();
        }

        /// <summary>
        /// 获取模板基本信息所有数据（测试专用）
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData()
        {
            B_Template a = new B_Template();
            var data = from i in _bb.GetAll().ToList()
                       join p in a.GetAll().ToList()
                       on i.TemplateID equals p.TemplateID
                       orderby i.BasicID ascending
                       select new Param_BasicInformation
                       {
                           BasicID = i.BasicID,
                           BasicName = i.BasicName,
                           Birth = i.Birth,
                           Tel = i.Tel,
                           Email = i.Email,
                           WorkingYears = i.WorkingYears,
                           IMG = i.IMG,
                           Sex = i.Sex,
                           Marriage = i.Marriage,
                           TemplateID = i.TemplateID,
                           Tname = p.Tname,
                       };
            return Ok(new Result<List<Param_BasicInformation>>
            {
                Data = data.ToList()
            });
        }

        /// <summary>
        /// 获取模板基本信息所有数据
        /// </summary>
        /// <returns></returns>
        [Route("GetData")]
        [HttpGet]
        public IHttpActionResult GetData(int page,int size)
        {
            B_Template a = new B_Template();
            var data = from i in _bb.GetAll().ToList()
                       join p in a.GetAll().ToList()
                       on i.TemplateID equals p.TemplateID
                       orderby i.BasicID ascending
                       select new Param_BasicInformation
                       {
                           BasicID = i.BasicID,
                           BasicName = i.BasicName,
                           Birth = i.Birth,
                           Tel = i.Tel,
                           Email = i.Email,
                           WorkingYears = i.WorkingYears,
                           IMG = i.IMG,
                           Sex = i.Sex,
                           Marriage = i.Marriage,
                           TemplateID = i.TemplateID,
                           Tname = p.Tname,
                       };
            return Ok(new Result<List<Param_BasicInformation>> {
                Data = data.Skip((page - 1)*size).Take(size).ToList(),
                Msg = data.Count().ToString() 
            });
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("Delete/{id}")]
        [HttpPost]
        public IHttpActionResult Delete(int id)
        {
            var data = _bb.GetAll().Where(x => x.BasicID == id);
            if (data.Count() > 0)
            {
                _bb.Delete(id);
                return Ok(new Result<object> { Code = 200 });
            }
            else
            {
                return Ok(new Result<object> { Msg = "数据为空" });
            }
        }

        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="param"></param>
        /// <returns></returns>
        [Route("Add")]
        [HttpPost]
        public IHttpActionResult Add(Param_BasicInformation param)
        {
            var data = new BasicInformation
            {
                BasicName = param.BasicName,
                Birth = param.Birth,
                Tel = param.Tel,
                Email = param.Email,
                WorkingYears = param.WorkingYears,
                IMG = param.IMG,
                Sex = param.Sex,
                Marriage = param.Marriage,
                TemplateID = param.TemplateID
            };
            _bb.Add(data);
            return Ok(new Result<object> { });
        }

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("FindByID/{id}")]
        [HttpGet]
        public IHttpActionResult FindByID(int id)
        {
            if (_bb.FindById(id)!=null)  
            {
                var data = _bb.FindById(id);
                return Ok(new Result<Param_BasicInformation>
                {
                    Data = new Param_BasicInformation
                    {
                        BasicID = data.BasicID,
                        BasicName = data.BasicName,
                        Birth = data.Birth,
                        Tel = data.Tel,
                        Email = data.Email,
                        WorkingYears = data.WorkingYears,
                        IMG = data.IMG,
                        Sex = data.Sex,
                        Marriage = data.Marriage,
                        TemplateID = data.TemplateID
                    },
                });
            }
            else
            {
                return Ok(new Result<object> { Msg = "数据为空", Code = 404 });
            };
        }

        /// <summary>
        /// 根据用户基本信息ID查询用户基本信息和其相关联的用户自定义信息
        /// </summary>
        /// <returns></returns>
        [Route("BasicInformAndCustomer/{id}")]
        [Route("BasicInformAndCustomer")]
        [HttpGet]
        public IHttpActionResult BasicInformAndCustomer(int id)
        {
            var param = _bb.GetAll();
            var basicCus = new B_BasicInformationCustom().GetAll();
            var listCus = from bb in param
                       join bc in basicCus on bb.BasicID equals bc?.BasicID
                       where bb.BasicID == id
                       select new Param_BasicInformCus
                       {
                           BICID = bc.BICID,
                           BICName = bc.BICName,
                           BICDescribe = bc.BICDescribe,
                           BasicID = bc.BasicID
                       };
            var data = from bb in param
                       where bb.BasicID == id
                       select new BasicInform_and_BasicInformCus
                       {
                           BasicID = bb.BasicID,
                           BasicName = bb.BasicName,
                           Birth = bb.Birth,
                           Tel = bb.Tel,
                           Email = bb.Email,
                           WorkingYears = bb.WorkingYears,
                           IMG = bb.IMG,
                           Sex = bb.Sex,
                           Marriage = bb.Marriage,
                           TemplateID = bb.TemplateID,
                           ListBasicInformCus = listCus == null ? null : listCus.Where(x => x.BasicID == bb.BasicID).ToList()
                       };
            return Ok(new Result<BasicInform_and_BasicInformCus>()
            {
                Data = data.FirstOrDefault()
            });
        }

        /// <summary>
        /// 根据模板ID查询用户信息和自定义信息
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [Route("FindByTemplateID/{id}")]
        [HttpGet]
        public IHttpActionResult FindByTemplateID(int id)
        {
            //var param = new B_BasicInformationCustom().GetAll();
            //var list = from bb in _bb.GetAll()
            //           join bc in param on bb.BasicID equals bc?.BasicID
            //           where bb.TemplateID == id
            //           select new Param_BasicInformCus
            //           {
            //               BICID = bc.BICID,
            //               BICName = bc.BICName,
            //               BICDescribe = bc.BICDescribe,
            //               BasicID = bc.BasicID
            //           };

            //var data = from bb in _bb.GetAll()
            //           where bb.TemplateID == id
            //           select new BasicInform_and_BasicInformCus
            //           {
            //               BasicID =bb.BasicID,
            //               BasicName = bb.BasicName,
            //               Birth = bb.Birth,
            //               Tel = bb.Tel,
            //               Email = bb.Email,
            //               WorkingYears = bb.WorkingYears,
            //               IMG = bb.IMG,
            //               Sex = bb.Sex,
            //               Marriage =bb.Marriage,
            //               TemplateID = bb.TemplateID,
            //               ListBasicInformCus= list==null?null: list.Where(x => x.BasicID == bb.BasicID).ToList()
            //           };

            var data = _bb.FindByTemplateID(id);
            if (data==null)
            {
                return Ok(new Result<IDAL.BasicInform_and_BasicInformCus>
                {
                    Code=404,
                    Msg="数据为空"
                });
            }
            else
            {
                return Ok(new Result<IDAL.BasicInform_and_BasicInformCus>
                {
                    Data = data
                });
            }
        }

        /// <summary>
        /// 更新
        /// </summary>
        /// <param name="id"></param>
        /// <param name="param"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("Update/{id}")]
        public IHttpActionResult Update(int id, BasicInformation param)
        {
            if (_bb.FindById(id)==null)
            {
                return Ok(new Result<object> { Msg = "不存在", Code = 404 });
            }
            _bb.Update(id, param);
            return Ok(new Result<object> { });
        }
    }
}
